package com.yantong.controller.api;

import com.yantong.controller.BaseController;
import com.yantong.result.ResultVo;
import com.yantong.utils.PageUtils;
import com.yantong.pojo.Address;
import com.yantong.pojo.Member;
import com.yantong.service.AddressService;
import com.yantong.service.MemberService;
import com.yantong.annotation.Auth;
import com.yantong.annotation.LoginUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.Map;


@Api(tags = "地址")
@Controller
@RequestMapping("/wxapi/address")
public class ApiAddressController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(ApiAddressController.class);
    @Autowired
    private AddressService addressService;

    @ApiOperation(value = "用户地址列表")
    @GetMapping("/list")
    @ResponseBody
    @Auth
    public ResultVo list(@ApiIgnore @LoginUser Member member,
                         @RequestParam(value = "mid") Long mid,
                         @RequestParam(value = "offset", required = false, defaultValue = "0") Integer offset,
                         @RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit) {
        Map params = new HashMap();
        params.put("mid", member.getId());
        Integer page = ((offset - 1) * limit) < 0 ? 0 : ((offset - 1) * limit);
        params.put("offset", page);
        params.put("limit", limit);

        PageUtils<Address> entityList = addressService.list(params);
        ResultVo resultVo = new ResultVo();
        resultVo.put("success", true);
        resultVo.put("data", entityList);
        return resultVo;
    }

    @ApiOperation(value = "地址详情")
    @GetMapping("/detail")
    @ResponseBody
//    @Auth
    public ResultVo detail(@RequestParam(value = "id") Long id) {
        Address address = addressService.getByKey(id);
        ResultVo resultVo = new ResultVo();
        resultVo.put("success", true);
        resultVo.put("data", address);
        return resultVo;
    }

    @ApiOperation(value = "添加地址")
    @PostMapping("/save")
    @ResponseBody
    @Auth
    public ResultVo save(@ApiIgnore @LoginUser Member member, @RequestBody Address address) {
        address.setMid(member.getId());

        if (addressService.save(address) < 0) {
            return ResultVo.error("添加地址失败！");
        }

        return ResultVo.ok("添加成功！");
    }

    @ApiOperation(value = "修改地址")
    @PostMapping("/update")
    @ResponseBody
    @Auth
    public ResultVo update(@ApiIgnore @LoginUser Member member, @RequestBody Address address) {
        if (address != null) {
            address.setMid(member.getId());
        } else {
            return ResultVo.error("修改地址信息失败！");
        }

        if (addressService.update(address) < 0) {
            return ResultVo.error("修改地址信息失败！");
        }

        return ResultVo.ok("修改成功！");
    }

    @ApiOperation(value = "获取用户最近的收货地址")
    @GetMapping("/getLateLyAddress")
    @ResponseBody
    @Auth
    public ResultVo getLateLyAddress(@ApiIgnore @LoginUser Member member,
                                     @RequestParam(value = "mid") Long mid,
                                     @RequestParam(value = "lat") String lat,
                                     @RequestParam(value = "lng") String lng) {
        Map params = new HashMap();
        params.put("mid", member.getId());
        params.put("lat", lat);
        params.put("lng", lng);
        Address address = addressService.getLateLyAddress(params);

        ResultVo resultVo = new ResultVo();
        resultVo.put("success", true);
        resultVo.put("data", address);
        return resultVo;
    }

    @ApiOperation(value = "删除地址")
    @GetMapping("/delete")
    @ResponseBody
    @Auth
    public ResultVo delete(@ApiIgnore @LoginUser Member member,
                           @RequestParam(value = "id") Long id) {
        addressService.delete(id);
        ResultVo resultVo = new ResultVo();
        resultVo.put("success", true);
        return resultVo;
    }


    /**
     * 小程序获取unid
     */
//    @ApiOperation(value = "获取token")
//    @GetMapping("/shang")
//    @ResponseBody
//    public ResultVo shang() {
//        String ticket = String.valueOf(new Random().nextInt(999999));
//        ResultVo resultVo = new ResultVo();
//        Member member = new Member();
//
//        member.setId(2560L);
//        member.setAppOpenid("1");
//        String sign = DigestUtils.md5Hex(member.getId() + member.getAppOpenid());
//        JSONObject autoToken = new JSONObject();
//        autoToken.put("id", member.getId());
//        autoToken.put("oid", member.getAppOpenid());
//        autoToken.put("sign", sign);
//        String authToken = Base64Utils.encodeToString(autoToken.toJSONString().getBytes(StandardCharsets.UTF_8));
//        resultVo.put("authToken", authToken);
////        redisTemplate.opsForValue().set("token:" + member.getId(), ticket);
//        //使用LoginUser注解需要
//        redisTemplate.opsForValue().set(authToken, member.getId().toString());
//        return resultVo;
//    }

}
